import Taro, { Component } from '@tarojs/taro'
import { View, Text, Button, Form, Input } from '@tarojs/components'
import Base from '@/components/Base'
import { ToastUtil } from '@/utils/toastUtil.js'
import { setToken } from '@/utils/request/auth'
import { login } from '@/utils/api';

import './index.scss'

class Index extends Component {
  config = {
    navigationBarTitleText: ''
  }

  state = {
    username: 'admin',
    password: 'admin2'
  }
  showToast() {
    ToastUtil.info('66666')
  }
  handleInput(key, e) {
    this.setState({
      [key]: e.detail.value
    })
  }
  login() {
    const { username, password } = this.state
    if (!username || !password) {
      ToastUtil.fail('请输入用户名或者密码')
      return false
    }
    login({ username, password }).then(res => {
      console.log('res', res)
      ToastUtil.success('登录成功')
      setToken(res.data.token)
      this.toSettingPage()
    }).catch(err => {
      ToastUtil.fail(err + '')
      console.log(err)
    })
  }

  toSettingPage() {
    Taro.navigateTo({
      url: '/pages/set/index'
    })
  }

  render() {
    const { username, password } = this.state
    return (
      <Base>
        <Form>
          <Input
            className="user-input"
            value={username}
            onInput={this.handleInput.bind(this, 'username')}
          ></Input>
          <Input
            className="user-input"
            value={password}
            onInput={this.handleInput.bind(this, 'password')}
          ></Input>
          <Button onClick={this.login}>登录</Button>
          <Button onClick={this.toSettingPage}>to setting page</Button>
        </Form>
      </Base>
    )
  }
}
export default Index
